local kuangdu = fk.CreateSkill{
  name = "lz__kuangdu",
}

Fk:loadTranslationTable{
  ["lz__kuangdu"] = "旷度",
  [":lz__kuangdu"] = "当你需要使用或打出【闪】时，你可令一名角色获得场上的一张牌，若该牌来自你的区域，则你视为使用或打出【闪】。",
  ["#lz__kuangdu-select"] = "旷度：选择场上的一张牌",
  ["#lz__kuangdu-target"] = "旷度：选择一名角色，其获得场上的一张牌",
}

local kuangdu_spec = {
  anim_type = "defensive",
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(kuangdu.name) and
      (data.cardName == "jink" or (data.pattern and Exppattern:Parse(data.pattern):matchExp("jink"))) then
      -- 检查是否有场上牌（装备区或判定区）
      return #table.filter(player.room:getAlivePlayers(), function(p) return #p:getCardIds("ej") > 0 end) > 0
    end
  end,
  on_cost = function(self, event, target, player, data)
    local to = player.room:askToChoosePlayers(player, {
      min_num = 1,
      max_num = 1,
      targets = player.room:getAlivePlayers(),
      skill_name = kuangdu.name,
      prompt = "#lz__kuangdu-target",
      cancelable = true,
    })
    if #to > 0 then
      event:setCostData(self, {tos = to})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).tos[1]
    local availableTargets = table.filter(room:getAlivePlayers(), function(p) return #p:getCardIds("ej") > 0 end)
    local to_get = player.room:askToChoosePlayers(to, {
      min_num = 1,
      max_num = 1,
      targets = availableTargets,
      skill_name = kuangdu.name,
      prompt = "#lz__kuangdu-select",
      cancelable = false,
    })
    if #to_get > 0 then
      local cid = room:askToChooseCard(player, {
        target = to_get[1],
        flag = "ej",
        skill_name = kuangdu.name,
      })
      room:obtainCard(to, cid, true, fk.ReasonPrey, player.id, kuangdu.name)
      if to_get[1] == player then
        local card = Fk:cloneCard("lvshi__jink")
        card.skillName = self.name
        local result = {
          from = player,
          card = card,
        }
        if event == fk.AskForCardUse then
          result.tos = {}
        end
        data.result = result
        return true
      end
    end
   end
}

kuangdu:addEffect(fk.AskForCardUse, kuangdu_spec)
kuangdu:addEffect(fk.AskForCardResponse, kuangdu_spec)

return kuangdu